package simu.nodes;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Timer;

import simu.tools.SimulateSensor;

public abstract class Node {
	
	private InetAddress nodeAddress;
	protected String[] files;
	private String FOLDER_NAME = "systemFiles"; 	// System folder to store files.
	private String name; 							// Id to identify uniquely of node.
	private SimulateSensor mySensorSimulator;  		// Object for simulate sensor.
	private Timer timer = new Timer();				// Get timer to simulate the files from sensors.
	String description = "Unknown Node";			// Description instance variable is set in each 
													// subclass and hold a description of the node, like
													// "I am a AP node".
	
	public abstract double cost();					//Abstract method; subclasses need to define their own implementation. 
	
	public Node(){
		this.FOLDER_NAME = "defaultNodes";
	}
	
	public Node(String SUBFOLDER_NAME, int ID){
		//Check if system folder exist, if not create.
		File f = new File(this.FOLDER_NAME);
		if (!f.exists()){
			f.mkdir();
		}
		//Check if type node folder exist, if not create.
		File f1 = new File(this.FOLDER_NAME+"/"+SUBFOLDER_NAME);
		if (!f1.exists()){
			f1.mkdir();
		}
		this.FOLDER_NAME = FOLDER_NAME+"/"+SUBFOLDER_NAME+"/"+Integer.toString(ID);
		//Check if node folder exist, if not create.
		File f2 = new File(this.FOLDER_NAME);
		if (!f2.exists()){
			f2.mkdir();
		}
			
	}
	
	public String getDescription(){
		return description;
	}
	
	public void startSensor(String sensor) throws IOException{
		mySensorSimulator = new SimulateSensor(sensor, this.FOLDER_NAME);
		long delay = 10*1000; 							//Save files each 5 seconds.
		timer.schedule(mySensorSimulator, 0, delay); 	//start recorder files by sensor.
		//System.out.println(myCreateFiles);			//Just for test
		
	}
	
	public void stopSensors(){
		timer.cancel();
	}
	
	public String[] getData(){
		File myFolder = new File(FOLDER_NAME);
		if (myFolder.isDirectory()){
			return myFolder.list();
		}
		return null;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return name;
	}

	public InetAddress getIP() {
		return nodeAddress;
	}

	public void setIP(InetAddress nodeAddress) {
		this.nodeAddress = nodeAddress;
	}

	public String getPath() {
		return this.FOLDER_NAME;
	}	

	
}
